﻿using Domain.Event;
using Domain.IRepositorys;
using Domain.Model;
using Domain.Services;
using Infrastructure.DomainEvent;
using Infrastructure.Error;
using Infrastructure.UniOfWorkBase;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Utility;

namespace Application.Event
{
    /// <summary>
    /// 支付任务事件
    /// </summary>
    public class PayTaskEventHandler : IDomainEventHandler<PayTaskEvent>
    {
        public IWalletRepository walletRepository { get; set; }
        public IUserRepository userRepository { get; set; }
        public IInviteLogRepository inviteLogRepository { get; set; }
        public IEFUnitOfWork efUnitOfWork { get; set; }
        public TaskService taskService { get; set; }
        public MsgPulishService msgPulishService { get; set; }
        public ILogger<PayTaskEventHandler> _logger { get; set; }
        public override Task HandleAysnc(PayTaskEvent domainEvent, CancellationToken cancellationToken = default)
        {
            try
            {
                var r = taskService.PayTask(domainEvent.UserID, domainEvent.TaskID, domainEvent.TaskAuditID);

                msgPulishService.SendMsg(domainEvent.UserID, "任务支付成功", $"任务支付成功,已提交审核!");

                domainEvent.Success();
            }
            catch (BizException ex)
            {
                domainEvent.Faild();
                //任务支付失败通知用户
                msgPulishService.SendMsg(domainEvent.UserID, "任务支付失败", $"任务支付失败,{ex.Message}!");

                _logger.LogException(ex, domainEvent);
            }


            return Task.CompletedTask;
        }
    }
}
